<template>
  <div class="upload-container">
    <slot>
      <el-button icon="el-icon-upload" @click="dialogVisible=true"></el-button>
    </slot>

    <el-dialog title="图片管理" :visible.sync="dialogVisible" :modal="false">
      <!-- <el-upload
        :multiple="true"
        :file-list="fileList"
        :show-file-list="true"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        :action="'/api/upload/'+ path +'/' + disk"
        :headers="headers"
        list-type="picture-card"
      >
        <i class="el-icon-plus"></i>
      </el-upload> -->
      <el-upload
        class="avatar-uploader"
        :multiple="false"
        :show-file-list="false"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        :action="'/api/upload/'+ path +'/' + disk"
        :headers="headers"
      >
        <img v-if="avatar" :src="avatar" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>

      </el-upload>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="default" @click="handleSubmit">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'
import Cookies from 'js-cookie'

export default {
  name: 'EditorSlideUpload',
  props: {
    color: {
      type: String,
      default: ''
    },
    path:{
        type:String,
        default:'goods'
    },
    disk:{
        type:String,
        default:'qiniu'
    },
    params:{
        type: String|Number,
        default: ''
    }
  },
  data() {
    return {
      headers:{'Authorization':'Bearer ' + Cookies.get('token')},
      dialogVisible: false,
      avatar:'',
      listObj: {},
      fileList: []
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      // const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      // if (!this.checkAllSuccess()) {
      //   this.$message('Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!')
      //   return
      // }
      this.$emit('successCBK',this.params,this.avatar)
      this.dialogVisible = false
    },
    handleSuccess(res, file) {
      console.log(res,file)
      if(res.errno === 1){
          this.$message.error(res.message);
          return
      }
      this.avatar = res.url
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      
      console.log('beforeUpload',file)
      //const isJPG = file.type === 'image/jpeg';
      const isLt1M = file.size / 1024 / 1024 < 1;
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!');
      // }
      if (!isLt1M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      //return isJPG && isLt1M;
      return isLt1M;
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader{
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
} 
</style>
